package com.lijie.ljblog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lijie.ljblog.mapper.ReplayMapper;
import com.lijie.ljblog.model.Mail;
import com.lijie.ljblog.model.Person;
import com.lijie.ljblog.model.Replay;
import com.lijie.ljblog.service.MailService;
import com.lijie.ljblog.service.ReplayService;
import com.lijie.ljblog.service.UtilService;
import com.lijie.ljblog.util.Result;
import com.lijie.ljblog.util.WebUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lijie
 * @since 2021-08-28
 */
@Service
public class ReplayServiceImpl extends ServiceImpl<ReplayMapper, Replay> implements ReplayService {

    @Autowired
    UtilService utilService;
    @Resource
    PersonServiceImpl personService;
    @Autowired
    MailService mailService;
    @Override
    public Result<Object> saveOrUpdateReplay(Replay replay, HttpServletRequest request) {
        String ip = WebUtils.getIpByRequset(request);
        Result<Map<String, Object>> locationByIP = utilService.getLocationByIP(ip);
        Result<Object> result = Result.getInstanceError();
        if (replay != null) {
            if (replay.getReplayPersonOne()!=null&&replay.getReplayPersonOne() > 0) {
                if(replay.getReplayPersonOne().equals(replay.getReplayPersonTwo())){
                    throw new RuntimeException("不能回复自己哦~");
                }
                replay.setIp(ip);
                if (locationByIP.isSuccess()) {
                    Map<String, Object> data = locationByIP.getData();
                    String address = MapUtils.getString(data, "address");
                    String jd = MapUtils.getString(data, "jd");
                    String wd = MapUtils.getString(data, "wd");
                    replay.setJd(jd);
                    replay.setLocation(address);
                    replay.setWd(wd);
                }
                boolean b = this.saveOrUpdate(replay);
                if(b) {
                    result.setSuccess();
                    //回复成功，发送邮件
                    Person emailTo = personService.getById(replay.getReplayPersonTwo());//被回复人
                    Person replayOne = personService.getById(replay.getReplayPersonOne());//回复人
                    Mail mail=new Mail();
                    mail.setEmail(emailTo.getEmail());//被回复人的邮箱
                    Map<String,Object> map=new HashMap<>();
                    map.put("replayMessage",replay.getMessage());//回复的输入数据
                    map.put("replayOneName",replayOne.getEmail());//回复人昵称
                    mail.setAttachment(map);
                    mail.setTitle("您在 什么是快乐 博客上留言被回复了");
                    mailService.sendTemplateMail(mail,"replayEmail");
                }else{
                    result.setMessage("保存回复信息数据库失败");
                }
            } else {
                result.setMessage("回复对象人为空");
            }
        } else {
            result.setMessage("回复为空");
        }
        return result;
    }

}
